from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, CheckConstraint, Text, TIMESTAMP, Enum
from sqlalchemy.orm import relationship, declarative_base, validates

Base = declarative_base()


class Users(Base):
    __tablename__ = "users"
    __comment__ = '用户表'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True, comment='递增主键')
    work_id = Column(Integer, nullable=False, unique=True, comment='工号')
    username = Column(String(50), nullable=False, comment='用户名')
    is_active = Column(Integer, default=1, nullable=False, comment='是否启用')
    created_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', nullable=False, comment='创建时间')
    update_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', onupdate='CURRENT_TIMESTAMP', nullable=False, comment='更新时间')


class RoleLevel(Base):
    __tablename__ = "roles_level"
    __comment__ = '角色等级表'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True, comment='低增主键')
    code = Column(String(255), unique=True, nullable=False, comment='级别编码')
    level = Column(Integer, nullable=False, comment='角色等级')
    name = Column(String(255), unique=True, nullable=False, comment='级别名称')
    description = Column(String(255), comment='级别描述')
    updater = Column(Integer, nullable=False, comment='修改人')
    is_active = Column(Integer, default=1, nullable=False, comment='是否启用')
    created_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', nullable=False, comment='创建时间')
    update_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', onupdate='CURRENT_TIMESTAMP', nullable=False, comment='更新时间')


class Roles(Base):
    __tablename__ = "roles"
    __comment__ = '角色表'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True, comment='递增主键')
    name = Column(String(255), unique=True, nullable=False, comment='角色名称')
    is_temporary = Column(Integer, default=1, nullable=False, comment='是否临时角色')
    description = Column(Text, nullable=False, comment='角色描述')
    level_code = Column(Integer, ForeignKey('level.code'), nullable=False, comment='级别编码')
    start_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', nullable=False, comment='角色生效开始时间')
    end_time = Column(TIMESTAMP, nullable=False, comment='角色生效结束时间')
    created_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', nullable=False, comment='创建时间')
    update_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', onupdate='CURRENT_TIMESTAMP', nullable=False, comment='更新时间')
    is_active = Column(Integer, default=1, nullable=False, comment='是否启用')
    
    # __table_args__ = (
    #     CheckConstraint('level BETWEEN 1 AND 3', name='check_level_range'),
    # )


class Permission(Base):
    __tablename__ = "permission"
    __comment__ = '权限表'
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True, comment='递增主键')
    en_name = Column(String(255), unique=True, nullable=False, comment='英文名称')
    name = Column(String(255), unique=True, nullable=False, comment='中文名称')
    description = Column(String(255), nullable=False, comment='描述')
    type = Column(Enum('2', '4', '8', '16', '32'), nullable=False, comment='权限类型,2查询|4新增|8修改|16删除|32空操作')
    is_active = Column(Integer, default=1, nullable=False, comment='是否可用') 
    updater = Column(Integer, nullable=False, comment='修改人')
    created_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', nullable=False, comment='创建时间')
    update_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', onupdate='CURRENT_TIMESTAMP', nullable=False, comment='更新时间')

    # __table_args__ = (
    #     CheckConstraint('level BETWEEN 1 AND 3', name='check_level_range'),
    # )

    @validates('type')
    def validate_type(self, key, value):
        valid_types = ['2', '4', '8', '16', '32']
        if value not in valid_types:
            raise ValueError(f"Invalid status: {value}")
        return value

class UserRole(Base):
    __tablename__ = "user_roles"
    user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
    role_id = Column(Integer, ForeignKey("roles.id"), primary_key=True)
    # updater = Column(String(255), nullable=False, comment='修改人')
    # created_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', nullable=False, comment='创建时间')
    # update_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', onupdate='CURRENT_TIMESTAMP', nullable=False, comment='更新时间')
    is_active = Column(Integer, default=1, nullable=False, comment='是否可用') 

class RolePermission(Base):
    __tablename__ = "role_permission"
    role_id = Column(Integer, ForeignKey("roles.id"), primary_key=True)
    permission_id = Column(Integer, ForeignKey("permission.id"), primary_key=True)
    # created_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', nullable=False, comment='创建时间')
    # update_time = Column(TIMESTAMP, server_default='CURRENT_TIMESTAMP', onupdate='CURRENT_TIMESTAMP', nullable=False, comment='更新时间')
    # updater = Column(String(255), nullable=False, comment='修改人')
    is_active = Column(Integer, default=1, nullable=False, comment='是否可用') 
